package org.hospital.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hospital.entity.Employee;
import org.hospital.entity.Log;
import org.hospital.entity.Project;
import org.hospital.service.LogService;
import org.hospital.service.PermissionService;
import org.hospital.service.ProjectService;
import org.hospital.util.Constant;
import org.hospital.util.DateUtil;
import org.hospital.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.WebRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("projectController")
@SessionAttributes(value = "employee", types = Employee.class)
public class ProjectController {
	
	@Autowired
	private ProjectService pjService;
	@Autowired
	private PermissionService pmService;
	@Autowired
	private LogService lService;
	
	/**
	 * 获取所有治疗项目信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllTreatmentProject", produces = "text/json;charset=utf-8")
	public String getAllTreatmentProject(HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		JSONArray resultJA = pjService.getTreatTable();
		//System.out.println("pjController.getAllTreatProject : "+ JSON.toJSONString(resultJA, true));
		return StringUtil.setResult(200, "获取成功", resultJA);
	}
	
	/**
	 * 获取所有费用项目信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllFundProject", produces = "text/json;charset=utf-8")
	public String getAllFundProject(HttpSession session) {
		Employee operator = (Employee) session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		JSONArray resultJA = pjService.getFundTable();
		//System.out.println("pjController.getAllFundProject : "+ JSON.toJSONString(resultJA, true));
		return StringUtil.setResult(200, "获取成功", resultJA);
	}
	
	/**
	 * 创建项目
	 * @param req
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/createProject", produces = "text/json;charset=utf-8")
	public String createProject(WebRequest req,HttpSession session){
		//验证权限
		Employee operator = (Employee)session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(operator.getPositionId(), Constant.CREATE_PROJECT.getName(), 5)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		
		String name = req.getParameter("name");
		String category = req.getParameter("category");
		double unitPrice = 0.0;
		double doctorCost = 0.0;
		double clinicCost = 0.0;
		double excutorCost = 0.0;
		int type = 0;
		int status = 0;
		if(category.equals("treatmentProject")) {
			//创建治疗项目
			unitPrice = Double.parseDouble(req.getParameter("unitPrice"));
			doctorCost = Double.parseDouble(req.getParameter("doctorCost"));
			clinicCost = Double.parseDouble(req.getParameter("clinicCost"));
			excutorCost = Double.parseDouble(req.getParameter("excutorCost"));
		}else {
			//创建费用项目
			type = Integer.parseInt(req.getParameter("type"), 10);
		}
		
		Project pj = new Project();
		pj.setCreatorId(operator.getEmployeeId());
		pj.setName(name);
		pj.setCategory(category);
		pj.setUnitPrice(unitPrice);
		pj.setDoctorCost(doctorCost);
		pj.setClinicCost(clinicCost);
		pj.setExcutorCost(excutorCost);
		pj.setType(type);
		pj.setStatus(status);
		pj.setAlterTime(new Timestamp(DateUtil.getNowLong()));
		pj.setCreateTime(new Timestamp(DateUtil.getNowLong()));
		
		pj = pjService.addEntity(pj);
		
		Log log = new Log();
		log.setEmployeeId(operator.getEmployeeId());
		log.setOperate(Constant.CREATE_PROJECT.getName());
		log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		log.setContent(JSON.toJSONString(pj));
		lService.addEntity(log);
		
		return StringUtil.setResult(200, "创建成功", pj);
	}
	
	/**
	 * 修改项目信息
	 * @param req {projectId,name,category,unitPrice,doctorCost,clinicCost,excutorCost,type,status}
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/alterProject", produces = "text/json;charset=utf-8")
	public String alterProject(WebRequest req,HttpSession session) {
		//验证权限
		Employee operator = (Employee)session.getAttribute("employee");
		if (operator == null) {
			return StringUtil.setResult(405, "请重新登陆", "");
		}
		if (!pmService.isPermisionEnough(operator.getPositionId(), Constant.ALTER_PROJECT.getName(), 5)) {
			return StringUtil.setResult(402, "权限不足", "");
		}
		
		//获取参数
		String projectIdStr = req.getParameter("projectId");
		String name = req.getParameter("name");
		String category = req.getParameter("category");
		String unitPriceStr = req.getParameter("unitPrice");
		String doctorCostStr = req.getParameter("doctorCost");
		String clinicCostStr = req.getParameter("clinicCost");
		String excutorCostStr = req.getParameter("excutorCost");
		String typeStr = req.getParameter("type");
		String statusStr = req.getParameter("status");
	
		if(null == projectIdStr || "" == projectIdStr) {
			return StringUtil.setResult(401, "缺少必要参数projectId", "");
		}
		Project pj = pjService.getById(Integer.parseInt(projectIdStr, 10));//目标项目
		if(pj == null) {
			return StringUtil.setResult(403, "不存在projectId", "");
		}
		if(null != name && "" != name) {
			pj.setName(name);
		}
		if(category.equals("treatmentProject")) {
			pj.setCategory(category);
		}
		if(category.equals("fundProject")) {
			pj.setCategory(category);
		}
		
		if(null != unitPriceStr && "" != unitPriceStr) {
			pj.setUnitPrice(Double.parseDouble(unitPriceStr));
		}
		if(null != doctorCostStr && "" != doctorCostStr) {
			pj.setDoctorCost(Double.parseDouble(doctorCostStr));
		}
		if(null != clinicCostStr && "" != clinicCostStr) {
			pj.setClinicCost(Double.parseDouble(clinicCostStr));
		}
		if(null != excutorCostStr && "" != excutorCostStr) {
			pj.setExcutorCost(Double.parseDouble(excutorCostStr));
		}
		if(null != typeStr && "" != typeStr) {
			int type = Integer.parseInt(typeStr, 10);
			if(type != 0 && type != 1) {
				return StringUtil.setResult(403, "type参数格式有误", "");
			}
			pj.setType(type);
		}
		if(null != statusStr && "" != statusStr) {
			int status = Integer.parseInt(statusStr, 10);
			if(pj.getCategory().equals("treatmentId") && status != 0) {
				return StringUtil.setResult(403, "status参数格式有误", "");
			}
			if(status != 0 && status != 1) {
				return StringUtil.setResult(403, "status参数格式有误", "");
			}
			pj.setStatus(status);
		}
		pj.setAlterTime(new Timestamp(DateUtil.getNowLong()));
		
		//数据库操作
		pj = pjService.addEntity(pj);
		
		//记录日志
		Log log = new Log();
		log.setEmployeeId(operator.getEmployeeId());
		log.setOperate(Constant.ALTER_PROJECT.getName());
		log.setOperateTime(new Timestamp(DateUtil.getNowLong()));
		log.setContent(JSON.toJSONString(pj));
		lService.addEntity(log);
		
		return StringUtil.setResult(200, "操作成功", "");
	}
	
}
